//
//  MineViewController.m
//  体育商城
//
//  Created by  on 2022/9/24.
//

#import "MineViewController.h"
#import "MineCell.h"
#import "LoginViewController.h"
#import "PersonInfoViewController.h"
#import "AddressManagerViewController.h"
#import "FavoriteViewController.h"
#import "OrderViewController.h"
#import "PrivateViewController.h"

@interface MineViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *avatar;
@property (strong, nonatomic) IBOutlet UIButton *nameBtn;
@property (strong, nonatomic) IBOutlet UILabel *age;
@property (strong, nonatomic) IBOutlet UIButton *editBtn;
@property (strong, nonatomic) IBOutlet UIView *bgView;
@property (strong, nonatomic) IBOutlet UIButton *allBtn;
@property (strong, nonatomic) IBOutlet UIButton *payBtn;
@property (strong, nonatomic) IBOutlet UIButton *ShipBtn;
@property (strong, nonatomic) IBOutlet UIButton *receiptBtn;
@property (strong, nonatomic) IBOutlet UIButton *saleBtn;
@property (strong, nonatomic) IBOutlet UIButton *logoutBtn;
@property (strong, nonatomic) IBOutlet UIButton *cancelBtn;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *nameBottom;
@property (strong, nonatomic)  NSArray *array;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)initUI{
    [self setupNaviBarColor:[UIColor colorHex:@"#FFFFFF"] titleColor:[UIColor blackColor]];
    
    self.editBtn.hidden = [APP.user.ID isEqualToString:@"去登录"];
    self.age.hidden = self.editBtn.hidden;
    self.logoutBtn.hidden = self.editBtn.hidden;
    self.cancelBtn.hidden = self.editBtn.hidden;
    self.nameBottom.constant = [APP.user.ID isEqualToString:@"去登录"] ? 11:-4;
    self.age.text = self.editBtn.hidden ? @"" : [NSString stringWithFormat:@"%@  %@",APP.user.age,LS(APP.user.gender)];
    self.avatar.image = self.editBtn.hidden ? [UIImage imageNamed:@"默认头像"] : [UIImage imageWithData:APP.user.headImg];
    NSString *name = self.editBtn.hidden ? LS(@"去登录") : (APP.user.username.length > 0 ? APP.user.username : APP.user.account);
    [self.nameBtn setTitle:LS(name) forState:UIControlStateNormal];
    
    self.bgView.layer.masksToBounds = NO;
    self.bgView.layer.shadowColor = [UIColor colorHex:@"#9B9B9B"].CGColor;
    self.bgView.layer.shadowOffset = CGSizeMake(0, 3);
    self.bgView.layer.shadowRadius = 10;
    self.bgView.layer.cornerRadius = 10;
    self.bgView.layer.shadowOpacity = 1;
    
    self.array = @[@{@"img":@"地址管理",@"title":@"地址管理"},@{@"img":@"收藏",@"title":@"收藏"},@{@"img":@"隐私协议",@"title":@"隐私协议"}];
    
    self.tableViewStyle = UITableViewStylePlain;
    self.tableView.scrollEnabled = NO;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerNib:[MineCell nib] forCellReuseIdentifier:[MineCell ID]];
    NSLayoutConstraint *top = [self.tableView a1:NSLayoutAttributeTop e:NSLayoutRelationEqual t:self.bgView a2:NSLayoutAttributeBottom c: 20];
    NSLayoutConstraint *left = [self.tableView a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeLeft c:0];
    NSLayoutConstraint *right = [self.tableView a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeRight c:0];
    NSLayoutConstraint *bottom = [self.tableView a1:NSLayoutAttributeBottom e:NSLayoutRelationEqual t:self.logoutBtn a2:NSLayoutAttributeTop c:-20];
    [self.view addConstraints:@[top,left,right,bottom]];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    [self action:sender.currentTitle];
}
- (void)action:(NSString *)text{
    if ([text isEqualToString:LS(@"去登录")]) {
        LoginViewController *vc = [LoginViewController new];
        vc.isLogin = YES;
        return [self.navigationController pushViewController:vc animated:YES];
    }
    if (APP.user.account ==  nil || APP.user.account.length <= 0) {
        return [Toast msg:LS(@"您还未登录，请先去登录")];
    }
    
    if ([text isEqualToString:LS(@"个人资料")]){
        if ([APP.user.ID isEqualToString:@"去登录"]) {
            return [self action:LS(@"去登录")];
        }
        PersonInfoViewController *vc = [PersonInfoViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([text isEqualToString:LS(@"退出登录")]){
        APP.user = [User new];
    }else if ([text isEqualToString:LS(@"注销账号")]){
        [AlertPopViewController pop:LS(@"注销账号") content:LS(@"您确定要注销账号吗？") type:AlertPopViewControllerTypeCancel block:^(int tag, NSString * _Nonnull text) {
            [UserTool delete:APP.user complete:^(NSDictionary * _Nonnull dict) {
                int code = [dict[@"code"] intValue];
                if (code == 200) {
                    APP.user = [User new];
                }
            }];
        }];
    }else if ([text isEqualToString:LS(@"地址管理")]){
        AddressManagerViewController *vc = [AddressManagerViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([text isEqualToString:LS(@"收藏")]){
        FavoriteViewController *vc = [FavoriteViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([text isEqualToString:LS(@"隐私协议")]){
        PrivateViewController *vc  = [PrivateViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([@[LS(@"全部"),LS(@"待付款"),LS(@"待发货"),LS(@"退款/售后"),LS(@"待收货")] containsObject:text]){
        OrderViewController *vc = [OrderViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)addNotify{
    [super addNotify];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionForUpdateUser) name:notifyUserUpdate object:nil];
}
- (void)actionForUpdateUser{
    [self viewDidLoad];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.array[indexPath.row];
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:[MineCell ID]];
    cell.img.image = [UIImage imageNamed:dict[@"img"]];
    cell.title.text = LS(dict[@"title"]);
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.array[indexPath.row];
    [self action:LS(dict[@"title"])];
}

@end
